package com.huarun.module.webapi.facePlus;

import com.alibaba.fastjson.JSONObject;
import com.huarun.common.util.HttpRequestUtils;
import com.huarun.common.util.MD5Util;
import com.huarun.common.util.PropertyUtil;
import com.huarun.module.webapi.IChunSunServiceApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("chunSunServiceApi")
public class ChunSunServiceApiImpl implements IChunSunServiceApi {

	protected Logger log = LoggerFactory.getLogger(getClass().getName());

	@Override
	public Map<String, Object> chunsunGetEmployee(String phone) {
		long time = System.currentTimeMillis();
		String sign_key = PropertyUtil.getProperty("sign_key");// 约定密钥
		Map<String, Object> returnMap = new HashMap<>();
		Map<String,Object> map = new HashMap<>();
		map.put("time", time);
		map.put("mobile", phone);
		com.google.gson.Gson gson = new com.google.gson.Gson();
		String str = gson.toJson(map);
		str = str + "&key=" + sign_key;
		String token = MD5Util.crypt(str).toUpperCase();
		JSONObject jsonEmployee = new JSONObject();
		
		jsonEmployee.put("sign", token);
		jsonEmployee.put("time", time);
		jsonEmployee.put("mobile", phone);
		//拼写url
		String url = PropertyUtil.getProperty("visitor_a_url") + PropertyUtil.getProperty("chunsun_employee_url");
		// 根据电话号码获取用户信息
		JSONObject employee = HttpRequestUtils.httpPost(url,jsonEmployee);
		System.out.println("==============春笋访客返回:" + (employee == null ? "null" : employee.toJSONString()));
		if (null != employee) {
			Map<String, Object> subMap = employee;
			int errorCode = (int) subMap.get("errcode");
			Map<String, Object> employeMap = (Map<String, Object>) subMap.get("Retdata");
			if (null != employeMap && null != employeMap.get("flag")) {
				if ((Integer) (employeMap.get("flag")) == 1 && errorCode == 0) {
					returnMap.put("companyName", employeMap.get("companyName"));
					returnMap.put("empName", employeMap.get("empName"));
					returnMap.put("tel", employeMap.get("tel"));
					returnMap.put("floor", employeMap.get("floor"));
				} else {
					log.info(phone+"在春笋中未查询到用户信息");
				}
			}
		}
		return returnMap;
	}

}
